package domain;

import domain.enums.BowlingFrameType;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import util.ScoreUtil;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class BowlingFrame extends Frame{
    private BowlingFrameType type = BowlingFrameType.NORMAL; // 默认值
    private Integer firstScore;
    private Integer secondScore;

    public void setFirstScore(int score) {
        if (ScoreUtil.isValidScore(score) && secondScore == null) {
            this.firstScore = score;
            if (ScoreUtil.isMaxScore(score)) type = BowlingFrameType.STRIKE;
        }
    }

    public void setSecondScore(int score) {
        if (type == BowlingFrameType.STRIKE){
            this.secondScore = 0;
            return;
        }
        if (ScoreUtil.isValidScore(score) && firstScore != null && ScoreUtil.isValidScore(firstScore + score)) {
            this.secondScore = score;
            if (ScoreUtil.isMaxScore(score + firstScore)) type = BowlingFrameType.SPARE;
        }
    }

    @Override
    public boolean isValid() {
        return firstScore != null && secondScore != null;
    }
}
